﻿using System.Linq;
using Sklep.Domain.Abstract;
using Sklep.Domain.Entities;

namespace Sklep.Domain.Concrete
{
    public class EFProductRepository : IProductRepository
    {
        private readonly EFDbContext _context = new EFDbContext();
        public IQueryable<Product> Products
        {
            get { return _context.Products; }
        }

        public void SaveProduct(Product product)
        {
            if (product.ProductID == 0)
                _context.Products.Add(product);
            else
            {
                Product productToUpdate = _context.Products.First(p => p.ProductID == product.ProductID);

                productToUpdate.Category = product.Category;
                productToUpdate.Description = product.Description;
                productToUpdate.Name = product.Name;
                productToUpdate.Price = product.Price;
                productToUpdate.ImageData = product.ImageData;
                productToUpdate.ImageMimeType = product.ImageMimeType;
            }

            _context.SaveChanges();
        }

        public void DeleteProduct(Product product)
        {
            _context.Products.Remove(product);
            _context.SaveChanges();
        }
    }
}
